package com.mvp.dagger.translate.mvp.model;

import android.telephony.mbms.FileInfo;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mvp.dagger.translate.base.OnHttpCallBack;
import com.mvp.dagger.translate.bean.TranslateResult;
import com.mvp.dagger.translate.mvp.contract.MainContract;
import com.mvp.dagger.translate.tool.WebSite;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import okhttp3.Call;

public class MainModel implements MainContract.Model {

    @Inject
    public MainModel() {
    }

    @Override
    public void getTranslate(Map<String, String> params, final OnHttpCallBack callBack) {
        OkHttpUtils.get()//
                .url(WebSite.translateUrl)//
                .params(params)//
                .build()//
                .connTimeOut(20000)
                .readTimeOut(20000)
                .writeTimeOut(20000)
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        callBack.onFail("请求失败"+e.toString());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        TranslateResult translateResult = gson.fromJson(response, TranslateResult.class);
                        callBack.onSuccessful(translateResult);
                    }
                    @Override
                    public void inProgress(float progress, long total, int id)
                    {
                        //			Log.e(TAG, "inProgress:" + progress);
                        //			mProgressBar.setProgress((int) (100 * progress));
                    }
                });
    }
}
